﻿@page "/Assembly"
@using System.IO;
@inject IFileReaderService fileReaderService
<h1>Hello, .NET standard assemblies!</h1>

This demo reads a .net Standard assembly and takes a look inside.
<br />
<br />
<strong>Note:</strong> This demo requires the full <a href="https://www.nuget.org/packages/NETStandard.Library/">.net standard dll</a>
 to be loaded, so this demo only runs if the linker does not remove the netstandard.dll.
<br />
<br />
<input type="file" @ref=inputElement accept=".dll,.exe" />
<button @onclick=ReadFile class="btn btn-primary">Read assembly</button>
<br />
<br />
<textarea style="max-width: 100%;" cols="60" rows="20">@Output</textarea>
@code {
    ElementReference inputElement;

    string Output { get; set; }

    public async Task ReadFile()
    {
        // This tricks the linker into keeping netstandard. Using the LinkerConfig.xml is currently not enough.
        string HackToTrickLinkerIntoKeepingDotNetStandard = typeof(System.Web.HttpUtility).FullName.Substring(0,0);
        Output = string.Empty;
        this.StateHasChanged();
        var nl = Environment.NewLine;
        foreach (var file in await fileReaderService.CreateReference(inputElement).EnumerateFilesAsync())
        {
            var fileInfo = await file.ReadFileInfoAsync();
            Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Name)}: {fileInfo.Name}{nl}";
            Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Size)}: {fileInfo.Size}{nl}";
            Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.Type)}: {fileInfo.Type}{nl}";
            Output += $"{nameof(IFileInfo)}.{nameof(fileInfo.LastModifiedDate)}: {fileInfo.LastModifiedDate?.ToString() ?? "(N/A)"}{nl}";
            Output += $"Reading file...";
            this.StateHasChanged();
            System.Reflection.Assembly assembly;
            using (var fs = await file.CreateMemoryStreamAsync(4096))
            {
                Output += $"Done reading file {fileInfo.Name}.{nl}";
                Output += $"Loading assembly...{nl}";
                this.StateHasChanged();
                try
                {
                    assembly = System.Reflection.Assembly.Load(fs.ToArray());
                    Output += $"Assembly loaded.{nl}";
                    Output += $"Assembly FullName:{assembly.FullName}{nl}";
                    this.StateHasChanged();
                }
                catch (Exception e)
                {
                    Output += $"Assembly load failed: {e}";
                    this.StateHasChanged();
                    return;
                }

            }
            Output += $"Loading assembly contents...{nl}";
            try
            {
                Output += $"Public Types:{nl}";
                Output += string.Join(nl, assembly.GetExportedTypes().Select(x => x.FullName));
            }
            catch (Exception e)
            {
                Output += $"Assembly loading of types failed: {e}";
                this.StateHasChanged();
                return;
            }
        }
    }
}



